
import 'package:flutter/cupertino.dart';
import 'package:ycbl/model/bl_error_entity.dart';
import 'package:ycbl/utils/log_util.dart';
import 'package:ycbl/widget/provider/view_state.dart';
import 'package:ycbl/widget/provider/view_state_widget.dart';

/// 状态改变通知基类
class BaseViewStateModel with ChangeNotifier {

  /// 防止页面销毁后，异步任务（如网络请求）才完成，再通知刷新导致报错
  bool _disposed = false;

  /// 当前页面的状态,默认为 idle,可在构造方法中指定;
  ViewState _viewState;

  /// get 方法，获取当前状态
  ViewState get viewState => _viewState;

  /// set 方法，更改状态，通知观察者
  set viewState(ViewState viewState) {
    _viewState = viewState;
    notifyListeners();
  }

  /// 子类可以在构造函数指定需要的页面状态
  BaseViewStateModel({ViewState viewState}) : this._viewState = viewState ?? ViewState.idle;

  /// 出错时的message
  String _errorMessage;
  /// get errorMessage
  String get errorMessage => _errorMessage;

  /// 以下变量是为了代码书写方便,加入的变量.严格意义上讲,并不严谨
  bool get isBusy => viewState == ViewState.busy;

  bool get isIdle => viewState == ViewState.idle;

  bool get isEmpty => viewState == ViewState.empty;

  bool get isError => viewState == ViewState.error;

  /// 更改当前状态 内部对 viewState 的赋值 实际上是调用了 viewState 的 set 方法，会发出通知给观察者
  void setIdle() {
    _errorMessage = null;
    viewState = ViewState.idle;
  }

  void setBusy() {
    _errorMessage = null;
    viewState = ViewState.busy;
  }

  void setEmpty() {
    _errorMessage = null;
    viewState = ViewState.empty;
  }

  /// [e]分类Error和Exception两种
  void setError(e, {String message, StackTrace stackTrace}) {
    if (e is String) {
      _errorMessage = e;
    } else if (e is BLErrorEntity){
      _errorMessage = e.message;
    }else {
      _errorMessage = message ?? (e is Error ? e.toString() : e?.message ?? '未知错误');
    }
    LogUtil.e('$_errorMessage');
    viewState = ViewState.error;
  }

  @override
  String toString() {
    return 'BaseViewStateModel{_viewState: $viewState, _errorMessage: $_errorMessage}';
  }

  /// 重写 notifyListeners ，当页面销毁后 就不通知了
  @override
  void notifyListeners() {
    if (!_disposed) {
      super.notifyListeners();
    }
  }

  /// 使用了 ChangeNotifierProvider 的 create 方法 创建的 provider，
  /// 从控件树上移除的时候，会自动调用（观察者如 ChangeNotifier 的） dispose 方法
  @override
  void dispose() {
    print('view_state_model dispose');
    _disposed = true;
    onDisposed();
    super.dispose();
  }

  //可以给子类复写，比如取消网络操作、释放 controller 等
  void onDisposed(){

  }

  /// 简单通用的处理每种状态要显示的 Widget
  /// contentWidget: 有内容时要展示的 Widget
  Widget simpleStateWidget({Widget contentWidget, Widget busyWidget, VoidCallback onRetry, String buttonText}) {
    if(isBusy) {
      LogUtil.d('viewState busy');
      return busyWidget ?? ViewStateBusyWidget();
    } else if(isError) {
      LogUtil.d('viewState error');
      return ViewStateErrorWidget(onButtonPressed: onRetry,);
    }else if (isEmpty) {
      LogUtil.d('viewState empty');
//      return ViewStateEmptyWidget(onButtonPressed: onRetry,);
      return  buttonText != null&& onRetry != null? ViewStateEmptyWidget(buttonText: buttonText,
        onButtonPressed: onRetry,): ViewStateEmptyWidget();
    }else{
      LogUtil.d('viewState idle');
      return contentWidget ?? Container();
    }
  }
}